$(function(){
	findType();
});
function findType(){
	$("#typeSelect").html("");
	$("#typeId").html("");
	var url="../../comtype/query.action";
	var data=null;
	$.post(url,data,function(mes){
		$("#typeId").append("<option value=''>====请选择====</option>");
		$("#typeSelect").append("<option value=''>====请选择====</option>");
		$.each(mes,function(index,item){
			$("#typeId").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			$("#typeSelect").append("<option value="+item.typeId+">"+item.typeName+"</option>");
			load();
		});
	},"json");
}


function exit(){
	$("#comId").val("");
	$("#comNameInput").val("");
	$("#comUnit").val("");
	$("#ciSales").val("");
	$("#ciCost").val("");
	$("#ciTrade").val("");
	$("#ciRetail").val("");
	$("#typeId").val("");
	$("#img").attr({"src":""});
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table','upload'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit
	,upload = layui.upload;

	
	//普通图片上传
	var uploadInst = upload.render({
		elem: '#comImage'
			,url: '/upload/'
				,auto:0
				,choose: function(obj){
					//预读本地文件示例，不支持ie8
					obj.preview(function(index, file, result){
						$('#img').attr('src', result); //图片链接（base64）
						$("#img").css({"width":"35px","height":"35px"});
					});
				}
	});

	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'edit'){
			var url="../../commodity/findById.action";
			var data={"comId":da.comId};
			$.post(url,data,function(mes){
				layer.open({
					type: 1	 //此处以iframe举例
					,title: '数据修改'
						,area: ['600px', '490px']
				,offset: 'auto'
					,content:$('#addOrUpdate')
					,success: function(layero){
						$("#comId").val(mes.comId);
						$("#comNameInput").val(mes.comName);
						$("#comUnit").val(mes.comUnit);
						$("#ciSales").val(mes.ciSales);
						$("#ciTrade").val(mes.ciTrade);
						$("#ciRetail").val(mes.ciRetail);
						$("#typeId").val(mes.typeId);
						$("#ciCost").val(mes.ciCost);
						//$("#comImage").val(mes.comImage);
						$("#img").attr({"src":"../../"+mes.comImage});
						//渲染
						load();
					},end : function() {
						//弹框关闭的回调函数
						$("#addOrUpdate").hide();
						exit();
					}
				});

			},"json");
		}else if(obj.event === 'del'){
			layer.confirm('真的删除行么', function(index){
				var index = layer.load(0, {shade: false});
				var url="../../commodity/delete.action";
				var data={"comId":da.comId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.close(index);
						layer.msg("删除成功");
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}else if(obj.event === 'snBom'){
			//添加详情
			layer.open({
				type: 2	 //此处以iframe举例
				,title: '配置材料'
					,area: ['1000px', '490px']
			,offset: 'auto'
				,content:['../../page/cst/snbom.html?comId='+da.comId+'','on']
			,success:function(layero, index){
				var body = layer.getChildFrame('body', index);
				var iframe = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
				iframe.$("#bomCommont").html("这是"+da.comName+"的物料清单");
				
				
				setTimeout(function(){
					var url="../../snbom/findByComId.action";
					var data={"comId":da.comId};
					$.post(url,data,function(mes){
						$.each(iframe.$(".layui-table-col-special"),function(index,item){
							var name=$(item).next().children().html();
							if(name){
								$.each(mes,function(index,infos){
									if(name==infos.maId){
										iframe.$("#materInput").val(iframe.$("#materInput").val()+infos.maId+"@");
										var check=$(item).children().children(":first").get(0);
										$(check).attr({"checked":true});
										
										var urls="../../materials/findById.action";
										var datas={"maId":infos.maId};
										$.post(urls,datas,function(info){
											console.info("------------"+info.maName);
											iframe.$("#snBomBody").append("<tr>"
													+"<td class='purchasemaid'>"+info.maId+"</td>"
													+"<td>"+info.maName+"</td>"
													+"<td><input type='text' value="+infos.bomNumber+" id='number"+info.maId+"' class='materNumber' onkeyup='findkeyup(\""+info.maId+"\",\""+info.maMoney+"\")' onkeydown='findkeyup(\""+info.maId+"\",\""+info.maMoney+"\")'/></td>"
													+"<td>"+info.maUnit+"</td>"
													+"<td><button class='layui-btn layui-btn-danger layui-btn-xs' id='id"+info.maId+"' onclick='findDelete(\""+info.maId+"\",\""+info.maName+"\")'>删除</button></td>"
													+"</tr>");
										},"json");
										
										iframe.load();
									}
								});
								
							};
						});
						
						//调用子页面渲染的方法
						iframe.load();
					},"json");
				},100);
				
				
			},end : function() {
				//alert("关闭");
			}
			});
		}

	});
	var $ = layui.$, active = {
			reload: function(){
				var comName = $('#comName');
				var oneMoney = $('#oneMoney');
				var twoMoney = $('#twoMoney');
				var typeSelect = $('#typeSelect');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					comName: comName.val(),
					oneMoney: oneMoney.val(),
					twoMoney: twoMoney.val(),
					typeId: typeSelect.val(),
				}
				});
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('.demoTable #insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '增加商品'
				,area: ['600px', '490px']
		,offset: 'auto'
			,content:$('#addOrUpdate')
			,end : function() {
				$("#operUser").attr({"disabled":false});
				//弹框关闭的回调函数
				$("#addOrUpdate").hide();
				exit();
			}
		});
	});

});

function load() {
	var form = layui.form
	form.render();
}

function findComName(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#comNameInput").val()==null || $("#comNameInput").val()==''){
			 parent.layer.msg('商品名称不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#comNameYz").val("×");
		}else if($("#comNameInput").val().length>50){
			 parent.layer.msg('商品名称不能超过50长度', {
			      icon: 5,
			      anim :6
			  });
			 $("#comNameYz").val("×");
		}else{
			 $("#comNameYz").val("√");
		}
	});
}
function findComUnit(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#comUnit").val()==null || $("#comUnit").val()==''){
			 parent.layer.msg('商品单位不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#comUnitYz").val("×");
		}else if($("#comUnit").val().length>10){
			 parent.layer.msg('商品单位不能超过10长度', {
			      icon: 5,
			      anim :6
			  });
			 $("#comUnitYz").val("×");
		}else if($("#comUnit").val().length>10){
			 parent.layer.msg('商品单位不能超过10长度', {
			      icon: 5,
			      anim :6
			  });
			 $("#comUnitYz").val("×");
		}else{
			 $("#comUnitYz").val("√");
		}
	});
}
function findCiSales(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#ciSales").val()==null || $("#ciSales").val()==''){
			 parent.layer.msg('进货价不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciSalesYz").val("×");
		}else if(isNaN($("#ciSales").val())){
			 parent.layer.msg('进货价只能是数字', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciSalesYz").val("×");
		}else{
			 $("#ciSalesYz").val("√");
		}
	});
}
function findCiTrade(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#ciTrade").val()==null || $("#ciTrade").val()==''){
			 parent.layer.msg('批发价不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciTradeYz").val("×");
		}else if(isNaN($("#ciTrade").val())){
			 parent.layer.msg('批发价只能是数字', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciTradeYz").val("×");
		}else{
			 $("#ciTradeYz").val("√");
		}
	});
}
function findCiRetail(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#ciRetail").val()==null || $("#ciRetail").val()==''){
			 parent.layer.msg('零售价不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciRetailYz").val("×");
		}else if(isNaN($("#ciRetail").val())){
			 parent.layer.msg('零售价只能是数字', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciRetailYz").val("×");
		}else{
			 $("#ciRetailYz").val("√");
		}
	});
}
function findCiCost(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#ciCost").val()==null || $("#ciCost").val()==''){
			 parent.layer.msg('成本价不能为空', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciTradeYz").val("×");
		}else if(isNaN($("#ciCost").val())){
			 parent.layer.msg('成本价只能是数字', {
			      icon: 5,
			      anim :6
			  });
			 $("#ciCostYz").val("×");
		}else{
			 $("#ciCostYz").val("√");
		}
	});
}

function FromSubmit(){
	findCiRetail();
	findCiTrade();
	findCiSales();
	findComUnit();
	findComName();
	findCiCost();
	
	var countyz=0;
	$.each($(".fromYz"),function(index,item){
		if($(item).val()=="√"){
			countyz++;
		}
	});
	if(countyz==6){
		$("#myform").submit();
	}
}
function ss(){
	var slt=document.getElementById("typeId");
	if(slt.value==""){
		 parent.layer.msg('请选择商品类型', {
		      icon: 5,
		      anim :6
		  });
	return false;
	}
	return true;
	}
